import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/user/user_income_info_model.dart';
import 'package:tiktokadult/hj_page/mine_business/welfare/share_data_list_page.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class ShareDetailsPage extends StatefulWidget {
  const ShareDetailsPage({super.key});

  @override
  State<ShareDetailsPage> createState() => _ShareDetailsPageState();
}

class _ShareDetailsPageState extends State<ShareDetailsPage> {
  UserIncomeModel? _model;
  RefreshController? _controller;
  final incomeList = <IncomeData>[];
  int page = 1;
  bool isLoading = true;
  final dataSource = [];

  @override
  void initState() {
    super.initState();
    _model = Get.arguments;
    incomeList.add(IncomeData("总推广人数", _model?.totalInviteUserCount ?? '0'));
    incomeList.add(IncomeData("总推广收益", _model?.totalIncomeAmount ?? '0'));
    incomeList.add(IncomeData("今日推广人数", _model?.todayInviteUserCount ?? '0'));
    incomeList.add(IncomeData("今日推广收益", _model?.todayIncomeAmount ?? '0'));
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _fetchPageData();
    });
  }

  _fetchPageData({bool isRefresh = true}) async {
    if (isRefresh) {
      page = 1;
    }
    final res = await MineService.fetchAllIncomeList(page: 1);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
    }
    if (res?.hasNext ?? false) {
      _controller?.loadComplete();
    } else {
      _controller?.loadNoData();
    }
    dataSource.addAll(res?.inviteItemList ?? []);
    page += 1;
    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('推广数据'),
      ),
      body: Padding(
        padding: EdgeInsets.symmetric(horizontal: 10),
        child: pullYsRefresh(
          onLoading: (_) => _fetchPageData(isRefresh: false),
          onRefresh: (_) => _fetchPageData(),
          child: CustomScrollView(
            slivers: [
              SliverList.separated(
                itemCount: incomeList.length,
                itemBuilder: (_, index) => _buildIncomeItem(incomeList[index]),
                separatorBuilder: (_, index) => 10.sizeBoxH,
              ),
              SliverToBoxAdapter(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SizedBox(height: 10),
                    // 收益明细
                    Text(
                      "收益明细",
                      style: const TextStyle(color: const Color(0xffbec4d6), fontWeight: FontWeight.w500, fontSize: 18.0),
                    ),
                    SizedBox(height: 10),
                  ],
                ),
              ),
              () {
                if (isLoading)
                  return SliverToBoxAdapter(
                    child: SizedBox(
                      height: 300,
                      child: LoadingCenterWidget(),
                    ),
                  );
                if (dataSource.isEmpty) return SliverToBoxAdapter(child: SizedBox(height: 300, child: CErrorWidget()));
                return SliverList.builder(
                    itemCount: dataSource.length,
                    itemBuilder: (_, index) {
                      final item = dataSource[index];
                      return _buildRecordItem(item.userName, item.incomeAmount);
                    });
              }()
            ],
          ),
          onInit: (_) => _controller = _,
        ),
      ),
    );
  }

  Widget _buildRecordItem(String title, int value) {
    return Container(
      height: 35,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: const TextStyle(color: const Color(0xffffffff), fontWeight: FontWeight.w400, fontSize: 14.0),
          ),
          Text(
            "收益+$value元",
            style: const TextStyle(color: const Color(0xffE1351F), fontWeight: FontWeight.w400, fontSize: 14.0),
          ),
        ],
      ),
    );
  }

  Widget _buildIncomeItem(IncomeData item) {
    return Container(
      height: 65,
      padding: EdgeInsets.symmetric(horizontal: 10),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(4)),
        border: Border.all(color: const Color(0xff1e262e), width: 1),
        color: Color(0xFF131b23),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            item.title,
            style: const TextStyle(color: const Color(0xE5FFFFFF), fontWeight: FontWeight.w400, fontSize: 14.0),
          ),
          Text(
            "${item.value}",
            style: const TextStyle(color: const Color(0x8CFFFFFF), fontWeight: FontWeight.w500, fontSize: 20.0),
          ),
        ],
      ),
    );
  }
}
